package org.jgs1904.tieba.controller;

import lombok.extern.slf4j.Slf4j;
import org.jgs1904.tieba.entity.Comment;
import org.jgs1904.tieba.entity.Post;
import org.jgs1904.tieba.entity.User;
import org.jgs1904.tieba.enums.PostTypeEnum;
import org.jgs1904.tieba.service.PostService;
import org.jgs1904.tieba.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author junki
 * @date 2020/5/12 14:42
 */
@Controller
@RequestMapping("post")
@Slf4j
public class PostController {

    @Autowired
    private PostService postService;

    @RequestMapping("page")
    public String page(Post post, @RequestParam(required = false, defaultValue = "5") int pageSize, @RequestParam(required = false, defaultValue = "0") int pageNum, Model model) {
        log.info("查询条件对象：{}", post);
        Page<Post> page = postService.getPage(post, pageSize, pageNum);
        model.addAttribute("page", page);
        return "post/list";
    }

    @RequestMapping("detail")
    public String detail(Long id, String message, Model model) {
        Post post = postService.getOneById(id);
        model.addAttribute("post", post);
        model.addAttribute("comment", new Comment());
        model.addAttribute("message", message);
        return "post/detail";
    }

    @GetMapping("add")
    public String addPage(Model model) {
        model.addAttribute("types", PostTypeEnum.values());
        return "post/add";
    }

    @PostMapping("add")
    public String add(Post post, MultipartFile file, @SessionAttribute("onlineUser") User user, Model model) {

        post.setUser(user);

        // 判断文件是否为空
        if (file.isEmpty()) {
            return "file is empty";
        }

        // 获取文件保存路径的真实路径
        String realPath = "F:/sbdemoupload/images";

        // 获取原文件名
        String originalFilename = file.getOriginalFilename();

        // 生成随机文件名
        String filename = UUIDUtil.getUUID(8);
        if (originalFilename.contains(".")) {
            filename += originalFilename.substring(originalFilename.lastIndexOf("."));
        }

        // 创建文件对象
        File saveFile = new File(realPath + "/" + filename);

        // 判断路径是否存在
        if (!saveFile.getParentFile().exists()) {
            saveFile.getParentFile().mkdirs();
        }

        // 保存文件
        try {
            file.transferTo(saveFile);
            post.setImage(filename);
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }

        Post result = postService.add(post);

        if (result != null) {
            return "redirect:/post/page";
        } else {
            model.addAttribute("types", PostTypeEnum.values());
            model.addAttribute("post", post);
            model.addAttribute("message", "发帖失败！请稍后重试！");
            return "post/add";
        }

    }

}
